<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<title>番茄scrm 企业推广数据管理中心系统 登录页面</title>
		<!-- 样 式 文 件 -->
		<link rel="stylesheet" href="component/pear/css/pear.css" />
		<link rel="stylesheet" href="static/css/other/login.css" />
		<link rel="shortcut icon" href="/favicon.ico" />
	</head>
    <!-- 代 码 结 构 -->
	<body background="static/images/background2.svg" style="background-size: cover;">
		<form class="layui-form" action="javascript:void(0);">
			<div class="layui-form-item">
				<img class="logo" src="admin/images/logo.png" />
				<div class="title">番茄SCRM</div>
				<div class="desc">
					让每一条数据都更具有价值
				</div>
			</div>
			<div class="layui-form-item">
				<input placeholder="账 户 : admin " type="text" name="account" lay-verify="account" hover class="layui-input" />
			</div>
			<div class="layui-form-item">
				<input placeholder="密 码 : admin " type="password" name="password" lay-verify="password" hover class="layui-input" />
			</div>
			<div class="layui-form-item">
				<input placeholder="验证码 : " type="text" name="password" maxlength="4"  lay-verify="captcha" hover class="code layui-input layui-input-inline"  />
				<img src="admin/images/captcha.gif" class="codeImage" />
			</div>
<!--			<div class="layui-form-item">
				<input type="checkbox" name="" title="记住密码" lay-skin="primary" checked>
			</div>-->
			<div class="layui-form-item">
				<button type="button" class="pear-btn pear-btn-success login" lay-submit lay-filter="login">
					登 入
				</button>
			</div>
		</form>
		<!-- 资 源 引 入 -->
		<script src="component/layui/layui.js"></script>
		<script src="component/pear/pear.js"></script>
		<script src="config/tomatoscrm.js"></script>
		<script>
			layui.use(['form', 'jquery', 'button', 'popup','hash', 'context'], function() {
				var form = layui.form;
				var button = layui.button;
				var popup = layui.popup;
				var $ = layui.jquery;
				var context = layui.context;
				var pearHash = layui.hash;
				var basePath = context.get("base-path");

				form.verify({
					account: function(value, item) {
						if (value.length < 4) return '请输入完整账户';
					}
					,password: [
						/^[\S]{5,12}$/
						,'密码必须6到12位，且不能出现空格'
					],captcha: function(value){
						if(value.length < 4){
							return '验证码未输入完整';
						}
					}
				});

				form.on('submit(login)', function(data) {
					data.field.password=pearHash.md5(data.field.password);//调用加密算法加密传输
					var btn = button.load({elem: '.login'})
					$.ajax({
						url: basePath + '/api/auth/login',
						data: JSON.stringify(data.field),
						dataType: 'json',
						contentType: 'application/json',
						type: 'post',
						success: function(result) {
							if (result.succeeded) {
								btn.stop(function() {
									popup.success("登录成功", function() {
										location.href = "index.html";
										// 将 token 存储 localStore
										context.put("token","Bearer "+result.data.accessToken)
									});
								})
							} else {
								btn.stop(function() {
									popup.failure("登录失败");
								})
							}
						}
						,error:function(XMLHttpRequest){
							if(XMLHttpRequest.status==419 ||XMLHttpRequest.responseJSON.message=="CSRF token mismatch." )
							{
								layer.msg('长时间未操作，自动刷新后重试！',{icon: 5});
								setTimeout(function () {
									window.location.reload();
								}, 2000);
							}
							if(XMLHttpRequest.status==429 ||XMLHttpRequest.responseJSON.message=="Too Many Attempts." )
							{
								popup.failure('尝试次数太多，请一分钟后再试');
							}
							//alert ("XMLHttpRequest.status="+XMLHttpRequest.status+"\ntextStatus="+XMLHttpRequest.responseJSON.code);
						}
					})
					return false;
				});
			})
		</script>
	</body>
</html>
